package io.shqyang.done.slice.main.quick;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public abstract class QuickProvider<T extends Object> extends BaseItemProvider {

    private final List<T> data;
    private final int itemLayoutId;
    private AbilitySlice slice;
    public QuickProvider(AbilitySlice slice, List<T> data, int itemLayoutId) {
        this.slice = slice;
        this.data = data;
        this.itemLayoutId = itemLayoutId;
    }


    protected abstract void onBindComponent(Component component, int position);

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public T getItem(int position) {
        if (data != null && position >= 0 && position < data.size()){
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(itemLayoutId, null, false);
        } else {
            cpt = component;
        }
        onBindComponent(cpt, i);
        return cpt;
    }

    public List<T> getData() {
        return data;
    }

    public void bindText(Component component, int componentId, String text) {
        Text title = (Text) component.findComponentById(componentId);
        title.setText(text);
    }

    public void bindText(Component component, int componentId, int textId) {
        Text title = (Text) component.findComponentById(componentId);
        title.setText(textId);
    }

    public void bindVisible(Component component, int componentId, boolean visible) {
        Component c = component.findComponentById(componentId);
        c.setVisibility(visible ? Component.VISIBLE : Component.HIDE);
    }

    public void bindVisible(Component component, int componentId, Visibility visible) {
        Component c = component.findComponentById(componentId);
        if (visible == Visibility.VISIBLE) {
            c.setVisibility(Component.VISIBLE);
        } else if (visible == Visibility.INVISIBLE) {
            c.setVisibility(Component.INVISIBLE);
        } else {
            c.setVisibility(Component.HIDE);
        }
    }

    enum Visibility {
        VISIBLE, INVISIBLE, HIDE
    }

}
